﻿using MVVMCommand.Command;
using System;
using System.ComponentModel;
using System.Windows.Input;

namespace MVVMCommand.ViewModel
{
    public class FormatViewModel : INotifyPropertyChanged
    {
        private string formattedText;
        private string originalText;

        public ICommand FormatCommand { get; private set; }

        public FormatViewModel()
        {
            FormatCommand = new MvvmCommand(
                (parameter) => { FormatText(); },
                (parameter) => { return !String.IsNullOrEmpty(OriginalText); })
                .AddListener<FormatViewModel>(this, x => x.OriginalText)
                .AddListener<FormatViewModel>(this, x => x.FormattedText);
        }

        public string FormattedText
        {
            get { return formattedText; }
            set
            {
                formattedText = value;
                OnPropertyChanged("FormattedText");
            }
        }

        public string OriginalText 
        {
            get { return originalText; }
            set
            {
                originalText = value;
                OnPropertyChanged("OriginalText");
            }
        }

        private void FormatText()
        {
            FormattedText = String.Format("Formatted: {0}", OriginalText);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                this.PropertyChanged(this, e);
            }
        }
    }
}
